﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Input;

namespace chemicall
{
    public class NewMenu : MenuScreen
    {
        GameManager gM;

        public NewMenu()
            : base("Nouvelle partie")
        {
            gM = GameManager.getInstance();
            //TODO : rendre ça générique (relou, parcourir la liste de fichiers dans le dossier
            // Sauvegardes (à créer) et créer autant MenuItem
            string save1 = gM.LoadNameSave("../../../Sauvegarde1.txt");
            string save2 = gM.LoadNameSave("../../../Sauvegarde2.txt");
            MenuItem save1MenuEntry;
            MenuItem save2MenuEntry;
            
            if (String.Compare(save1, "New") != 0 && String.Compare(save2, "New") != 0)
            {
                MenuItem eraseMenuEntry = new MenuItem("Supprimer une partie ?");
                eraseMenuEntry.Selected += EraseMenuEntrySelected;
                MenuEntries.Add(eraseMenuEntry);                
            }
            else
            {
                if (String.Compare(save1, "New") == 0)
                {
                    save1MenuEntry = new MenuItem(save1);
                    save1MenuEntry.Selected += Save1MenuEntrySelected;
                    MenuEntries.Add(save1MenuEntry);
                }
                if (String.Compare(save2, "New") == 0)
                {
                    save2MenuEntry = new MenuItem(save2);
                    save2MenuEntry.Selected += Save2MenuEntrySelected;
                    MenuEntries.Add(save2MenuEntry);
                }
            }
            MenuItem backMenuEntry = new MenuItem("Retour");
            backMenuEntry.Selected += BackMenuSelected;
            MenuEntries.Add(backMenuEntry);
        }

        //TODO Et du coup, ne faire qu'une seule méthode avec un paramètre String (la save)
        void Save1MenuEntrySelected(object sender, System.EventArgs e)
        {
            this.ScreenManager.LastScreen = this;
            gM.fichier = "../../../Sauvegarde1.txt";
            ScreenManager.AddScreen(new ChooseName("../../../Sauvegarde1.txt"), PlayerIndex.One);
        }

        void Save2MenuEntrySelected(object sender, System.EventArgs e)
        {
            this.ScreenManager.LastScreen = this;
            gM.fichier = "../../../Sauvegarde2.txt";
            ScreenManager.AddScreen(new ChooseName("../../../Sauvegarde2.txt"), PlayerIndex.One);
        }

        void EraseMenuEntrySelected(object sender, System.EventArgs e)
        {
            this.ScreenManager.LastScreen = this;
            ScreenManager.AddScreen(new EraseMenu(), PlayerIndex.One);
        }

        void BackMenuSelected(object sender, System.EventArgs e)
        {
            ScreenManager.AddScreen(this.ScreenManager.LastScreen, PlayerIndex.One);
            this.ScreenManager.LastScreen = this;
        }
    }
}
